package com.example.shop.fgo.ui.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.shop.fgo.Constant;
import com.example.shop.fgo.R;
import com.example.shop.fgo.entity.DownloadUrlEntity;
import com.example.shop.fgo.entity.FileListEntity;
import com.example.shop.fgo.net.DownloadUrlServe;
import com.example.shop.fgo.ui.views.ProgressWheel;
import com.example.shop.fgo.utils.CookieUtil;
import com.example.shop.library.net.NetJsonUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import uk.co.senab.photoview.PhotoView;

/**
 * Created by Administrator on 2017/2/28.
 */

public class ItemVPDT extends PagerAdapter {
    private final DisplayImageOptions mOptions;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<FileListEntity.DataEntity> mDataEntities = new ArrayList<>();




    public ItemVPDT(Context context, List<FileListEntity.DataEntity> dataEntities) {
        mContext = context;
        mDataEntities = dataEntities;
        mLayoutInflater = LayoutInflater.from(context);
        mOptions = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.file_more)
                .showImageForEmptyUri(R.drawable.file_more)
                .showImageOnFail(R.drawable.file_more)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(false)
                .bitmapConfig(Bitmap.Config.RGB_565)
//                .displayer(new CircleBitmapDisplayer(90,5))   "不能加特效，加特效后photoView失效"
                .build();

    }
    
    
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = mLayoutInflater.inflate(R.layout.item_vp_dt, container, false);
        final FileListEntity.DataEntity dataEntity = mDataEntities.get(position);
        final ProgressWheel progressWheel = (ProgressWheel) view.findViewById(R.id.pw_spinner);
        final PhotoView mImgvItemVpDt = (PhotoView) view.findViewById(R.id.imgv_item_vp_dt);
        progressWheel.spin();
        /*做这部分的操作的原因是，防止我们的三级缓存不生效
        
            三级缓存：当我们使用url从网络上加载过该图片之后，将会在本地和内存各缓存一份，之后就不去网络重新获取了
            而直接使用本地或者内存的
            而本地和内存在存储我们的网络图片时，本地---文件名   内存 map  key  都和url有关系，那么如果每次url不相同
            就无法复用了
            
            我们现在就是不相同
            所以我们只获取一次url，之后直接拿来用
        
        */
        if (TextUtils.isEmpty(dataEntity.getDownloadUrl())) {
            DownloadUrlServe downloadUrlServe = NetJsonUtil.getRetrofit(Constant.BASE_URL, DownloadUrlServe.class);
            Call<DownloadUrlEntity> call = downloadUrlServe.request(dataEntity.getFid(), CookieUtil.getCookie());
            call.enqueue(new Callback<DownloadUrlEntity>() {
                @Override
                public void onResponse(Call<DownloadUrlEntity> call, Response<DownloadUrlEntity> response) {
                    DownloadUrlEntity downloadUrlEntity = response.body();
                    
                    String url = downloadUrlEntity.getUrl();
                    Logger.e(dataEntity.getN() + "  的原图链接   " + url);
                    dataEntity.setDownloadUrl(url.replace("http://101.200.183.103", "http://101.200.183.103:9999"));
                    ImageLoader.getInstance().displayImage(dataEntity.getDownloadUrl(), mImgvItemVpDt, mOptions, new ImageLoadingListener() {
                        @Override
                        public void onLoadingStarted(String s, View view) {
                            
                        }
                        
                        @Override
                        public void onLoadingFailed(String s, View view, FailReason failReason) {
                            progressWheel.stopSpinning();
                            progressWheel.setVisibility(View.GONE);
                        }
                        
                        @Override
                        public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                            progressWheel.stopSpinning();
                            progressWheel.setVisibility(View.GONE);
                        }
                        
                        @Override
                        public void onLoadingCancelled(String s, View view) {
                            progressWheel.stopSpinning();
                            progressWheel.setVisibility(View.GONE);
                        }
                    }, new ImageLoadingProgressListener() {
                        @Override
                        public void onProgressUpdate(String s, View view, int i, int i1) {
                            Logger.e(i+"　　　"+dataEntity.getS());
                            int progress = (int) (i * 100.0f / dataEntity.getS());
                            progressWheel.setText(progress + "");
                        }
                    });
                }
                
                @Override
                public void onFailure(Call<DownloadUrlEntity> call, Throwable t) {
                    
                }
            });
        } else {
            ImageLoader.getInstance().displayImage(dataEntity.getDownloadUrl(), mImgvItemVpDt, mOptions, new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String s, View view) {
            
                }
        
                @Override
                public void onLoadingFailed(String s, View view, FailReason failReason) {
                    progressWheel.stopSpinning();
                    progressWheel.setVisibility(View.GONE);
                }
        
                @Override
                public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                    progressWheel.stopSpinning();
                    progressWheel.setVisibility(View.GONE);
                }
        
                @Override
                public void onLoadingCancelled(String s, View view) {
                    progressWheel.stopSpinning();
                    progressWheel.setVisibility(View.GONE);
                }
            }, new ImageLoadingProgressListener() {
                @Override
                public void onProgressUpdate(String s, View view, int i, int i1) {
                    Logger.e(i+"　　　"+dataEntity.getS());
                    int progress = (int) (i * 100.0f / dataEntity.getS());
                    progressWheel.setText(progress + "");
                }
            });
            
        }
        
        container.addView(view);
        return view;
    }
    
    @Override
    public int getCount() {
        return mDataEntities.size();
    }
    
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
    
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
